package com.jugheadzhou.kafka.core.logger;

import com.jugheadzhou.kafka.core.base.KafkaMessage;
import com.jugheadzhou.kafka.core.enums.KafkaMessageTypeEnum;
import com.jugheadzhou.kafka.core.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.UUID;

/**
 * @author jugheadzhou
 * @date 2021-09-23
 */
@Slf4j
public class AsyncKafkaLogger {

    /**
     * 通过日志发送kafka消息
     *
     * @param typeEnum 消息类型
     * @param data     业务数据
     */
    public static void info(KafkaMessageTypeEnum typeEnum, Object data) {
        log.info("{}", wrapperKafkaMessage(typeEnum, data));
    }

    /**
     * 标准化转换Kafka消息体
     *
     * @param messageType 消息类型
     * @param data        业务数据
     */
    public static <T> String wrapperKafkaMessage(KafkaMessageTypeEnum messageType, T data) {
        KafkaMessage<T> msg = KafkaMessage.<T>builder()
                .key(UUID.randomUUID().toString())
                .type(messageType.getCode())
                .timestamp(System.currentTimeMillis())
                .data(data)
                .build();
        return JsonUtil.object2String(msg);
    }

}
